/**
 * Created by plter on 2016/11/10.
 */

(function () {

    var lrcObject;
    var player = document.querySelector("#player");
    var lrcShow = document.querySelector("#lrc-show");


    function tryToSyncLrc() {
        setInterval(function () {
            if (lrcObject) {
                var key = Math.round(player.currentTime);
                var value = lrcObject[key];

                if (value) {
                    lrcShow.innerHTML = value;
                }
            }
        }, 1000);
    }

    function init() {

        var xhr = new XMLHttpRequest();
        xhr.addEventListener("load", function (e) {
            var lrcContent = xhr.responseText;

            var p = /\[(\d{2}):(\d{2})\.(\d{2})\](.+)/g;

            lrcObject = {};
            var result;
            while (result = p.exec(lrcContent)) {
                lrcObject[parseInt(result[1]) * 60 + parseInt(result[2])] = result[4];
            }

            console.log(lrcObject);
        });
        xhr.open("GET", "Heartbeats.lrc");
        xhr.send();


        tryToSyncLrc();
    }

    init();
})();